package com.tao.demo.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.tao.demo.resolver.MultiRequestBodyArgumentResolver;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author DemoTao
 */

@Log4j2
@Configuration
public class WebConfig implements WebMvcConfigurer {
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    // 设置允许跨域的路径
    registry.addMapping("/**")
      // 设置允许跨域请求的域名
      .allowedOriginPatterns("*")
      // 是否允许cookie
      .allowCredentials(true)
      // 设置允许的请求方式
      .allowedMethods("*")
      // 设置允许的header属性
      .allowedHeaders("*")
      // 跨域允许时间
      .maxAge(3600);
    
    log.info("启动跨域设置完成!");
  }
  
  
  /**
   * 注册自定义的参数解析器
   */
  @Override
  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){
    argumentResolvers.add(new MultiRequestBodyArgumentResolver());
    WebMvcConfigurer.super.addArgumentResolvers(argumentResolvers);
  }
  
  @Bean
  public HttpMessageConverter<String> responseBodyConverter() {
    return new StringHttpMessageConverter(StandardCharsets.UTF_8);
  }
  
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    WebMvcConfigurer.super.configureMessageConverters(converters);
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    //设置日期格式
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");
    objectMapper.setDateFormat(smt);
    mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
    //设置中文编码格式
    List<MediaType> list = new ArrayList<>();
    list.add(MediaType.valueOf("application/json;charset=UTF-8"));
    mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
    converters.add(responseBodyConverter());
    converters.add(mappingJackson2HttpMessageConverter);
  }

  
}